package com.xd.common.base;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.PageHelper;
import com.xd.common.constant.CommonConstant;
import com.xd.common.model.Page;
import com.xd.common.model.R;
import com.xd.common.vo.PageInfo;
import com.xd.common.vo.Validity;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

/**
 * @author huboxin
 * @title: BaseController
 * @projectName bk
 * @description: 控制器父类
 * @date 2019/12/3013:40
 */


public class BaseController<E extends IService<T>, T, ID extends Serializable> extends ApiController implements BaseFeign<T, ID> {


    /**
     * 服务对象
     */
    @Autowired
    protected E service;


    /**
     * 查询所有数据
     *
     * @return 所有数据
     */

    @Override
    @PostMapping("selectAll")
    @ApiOperation(value = "查询所有数据")
    public R<List<T>> selectAll() {
        return result((List<T> list) -> list != null ? success(list) : success(CollUtil.newArrayList())
                , this.service.list(new QueryWrapper<T>().eq(CommonConstant.DEL_FLAG_COLUMN, CommonConstant.DEL_FLAG)));
    }


    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @PostMapping("selectByPage")
    @ApiOperation(value = "分页查询所有数据")
    @Override
    public R<IPage<T>> selectByPage(@ApiParam("分页对象") @RequestBody Page<T> page) {
        QueryWrapper<T> wrapper = new QueryWrapper<>(page.getT());
        wrapper.eq(CommonConstant.DEL_FLAG_COLUMN, CommonConstant.DEL_FLAG);
        //模糊查询
        if (CollUtil.isNotEmpty(page.getLikeColumns())) {
            page.getLikeColumns()
                    .forEach(wrapper::like);
        }
        IPage<T> iPage = this.service.page(page, wrapper);
        return success(iPage);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "通过主键查询单条数据", notes = "通过主键查询单条数据")
    @Override
    public R<T> selectOne(@PathVariable @ApiParam("id") ID id) {
        return result(ObjectUtil::isEmpty, this.service.getById(id), "未查询到信息");
    }

    /**
     * 新增数据
     *
     * @param t 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @Override
    public R<Boolean> insert(@RequestBody @Validated(Validity.Add.class) @ApiParam("实体对象") T t) {
        return result(Boolean::booleanValue, this.service.save(t), "添加失败");
    }

    /**
     * 修改数据
     *
     * @param t 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改数据", notes = "修改数据")
    @Override
    public R<Boolean> update(@RequestBody @Validated(Validity.Upload.class) @ApiParam("实体对象") T t) {
        return result(Boolean::booleanValue, this.service.updateById(t), "修改失败");
    }


    /**
     * 删除数据
     *
     * @param idList 主键集合
     * @return 删除结果
     */
    @PostMapping("delete")
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Boolean> delete(@ApiParam("主键集合") @RequestBody List<ID> idList) {
        return result(Boolean::booleanValue, this.service.removeByIds(idList), "删除失败");
    }

    /**
     * 删除单个数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("deleteOne")
    @ApiOperation(value = "删除单个数据", notes = "删除单个数据")
    @Override
    public R<Boolean> deleteOne(@RequestParam("id") @ApiParam("主键") ID id) {
        return result(Boolean::booleanValue, this.service.removeById(id), "删除失败");
    }

}
